-- 6.1.1 并列的列表内包与⼀般化的列表内包
{-# LANGUAGE ParallelListComp #-}
{-# LANGUAGE TransformListComp #-}

import GHC.Exts

series :: Int -> [Double]
series n = [1 / (2 * (fromIntegral k) + 1) * (-1) ^ k | k <- [0 .. n]]

ret = 4 * (sum $ series 200000)

series2 :: Int -> [Double]
series2 n = [(ta k) * (-1) ^ k + (tb k) * (-1) ^ k | k <- [0 .. n]]
  where
    ta k = (1 / 2) ^^ (fac k) / (fac k)
    tb k = (1 / 3) ^^ (fac k) / (fac k)
    fac k = 2 * (fromIntegral k) + 1

ret2 = 4 * (sum $ series2 100)

-- 并列的列表内包
te1 = [(x, y) | x <- [1, 2, 3] | y <- [4, 5, 6]]

te2 = [(x, y, z) | x <- [1, 2, 3], y <- [4, 5, 6] | z <- cycle [7, 8, 9]]

-- --------------- 一般化的列表内包
table =
  [ ("Hangzhou", "MP4", 243),
    ("Hangzhou", "CD", 925),
    ("Beijing", "MP4", 157),
    ("Beijing", "CD", 536),
    ("Shanghai", "MP4", 784),
    ("Shanghai", "CD", 766)
  ]

-- anaA =[ (the product, sum cost)  | (city, product, cost) <- table, then group by product]

analysis =
  [ (the product, sum cost)
    | (city, product, cost) <- table,
      then group by
        product
      using
        groupWith,
      then
        sortWith
      by
        (sum cost)
  ]
